﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace Simple2DSample
{
    public partial class MovingSprite
    {

        static public uint _UpdateCount = 0;
        public FSM _FSM = new FSM();
        public float _UpdateTime = 0;
        public int GetCountCollision()
        {
            int count = 0;
            foreach (MovingSprite obj in Simple2D.movingSpriteList)
            {
                if (obj != this && obj.srcRect.IntersectsWith(this.srcRect))
                    ++count;

            }
            return count;
        }

        

        /// <summary>
        /// This method moves, scales, and rotates the sprite's parameters during frame updates.
        /// </summary>
        /// <param name="time">Elapsed time in seconds of the last frame.</param>
        public IEnumerator Move(float time)
        {


            if (GetCountCollision() < 10)
            {
                yield break;
            }

            //ThreadJob job = new ThreadJob(GetCountCollision);
            //yield return job;
            //if (job.ret < 10)
            //{
            //    yield break;



            //}


            timeSinceTurn += time;
            aliveTime += time;

            //check to see if it is time to turn
            if (timeSinceTurn > timeToTurn)
            {
                //randomly turn within a 90 degree arc to a new direction
                direction += (.5f - (float)rand.NextDouble()) * PI * .5f;
                timeSinceTurn -= timeToTurn;
            }

            //calculate a new size for the output sprite.  The sprite will change scale relative to its source x/y scale.
            spriteWidth = Math.Abs((float)Math.Cos(aliveTime * 30f) * (float)srcRect.Width * .333f) + (float)srcRect.Width;
            spriteHeight = Math.Abs((float)Math.Sin(aliveTime * 2.5f) * (float)srcRect.Height * .333f) + (float)srcRect.Height;

            //Get a delta x/y position based on the speed time time times the magnitude of the unit direction
            float dX = speed * time * (float)Math.Cos(direction);
            float dY = speed * time * (float)Math.Sin(direction);

            //detect collisions (using the center point) with any of the screen adges and move back if the
            //sprite is out of bounds.
            if ((dX + position.X) > screenWidth) { direction = PI; dX = speed * time * (float)Math.Cos(direction); }
            if ((dY + position.Y) > screenHeight) { direction = 3f * PI / 2f; dY = speed * time * (float)Math.Sin(direction); }
            if ((dY + position.X) < 0) { direction = 0; dX = speed * time * (float)Math.Cos(direction); }
            if ((dY + position.Y) < 0) { direction = PI / 2f; dY = speed * time * (float)Math.Sin(direction); }

            position.X += dX;
            position.Y += dY;

            if (direction > (Math.PI * 2)) direction -= 2 * PI;


            _UpdateCount++;

            yield break;
        }


        public void Update(float time)
        {

            _UpdateTime += time;
            if (_FSM.m_ProcessEnum == null)
            {
                _FSM.m_ProcessEnum = Move(_UpdateTime);
                _UpdateTime = 0;
            }

            if (_FSM.m_Condition != null && _FSM.m_Condition.IsComplete() == false)
            {// 선행작업이 완료되지 않았다.
                return;
            }

            if (_FSM.m_ProcessEnum.MoveNext() == true)
            {

                _FSM.m_Condition = (ICondition)_FSM.m_ProcessEnum.Current;
            }
            else
            {
                _FSM.m_ProcessEnum = null;
                _FSM.m_Condition = null;
            }
            
        }

    }
}
